home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / wrlib / ppm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-14  |  3.5 KB  |  180 lines

  1. /* ppm.c - load PPM image from file
  2.  * 
  3.  *  Raster graphics library
  4.  * 
  5.  *  Copyright (c) 1997 Alfredo K. Kojima
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *  
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *  
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #include <config.h>
  23.  
  24.  
  25. #include <X11/Xlib.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #include "wraster.h"
  31.  
  32.  
  33. static RImage*
  34. load_graymap(char *file_name, FILE *file, int w, int h, int max, int raw)
  35. {
  36.     RImage *image;
  37.     
  38.     image = RCreateImage(w, h, 0);
  39.     if (!image) {
  40.     return NULL;
  41.     }
  42.     if (!raw) {
  43.     
  44.     } else {
  45.     if (max<256) {
  46.         int x, y;
  47.         char *buf, *ptr;
  48.         
  49.         buf = malloc(w);
  50.         if (!buf) {
  51.         return NULL;
  52.         }
  53.         
  54.         ptr = image->data;
  55.         for (y = 0; y < h; y++) {
  56.         if (!fgets(buf, w, file)) {
  57.             free(buf);
  58.             goto short_file;
  59.         }
  60.         for (x = 0; x < w; x++) {
  61.             *(ptr++) = buf[x];
  62.             *(ptr++) = buf[x];
  63.             *(ptr++) = buf[x];
  64.         }
  65.         }
  66.         free(buf);
  67.     } else {
  68.         
  69.     }
  70.     }
  71.     
  72.     return image;
  73.     
  74.   short_file:
  75.     RErrorCode = RERR_BADIMAGEFILE;
  76.     return NULL;
  77. }
  78.  
  79.  
  80. static RImage*
  81. load_pixmap(char *file_name, FILE *file, int w, int h, int max, int raw)
  82. {
  83.     RImage *image;
  84.     int i;
  85.     char buf[3];
  86.     char *ptr;
  87.     
  88.     image = RCreateImage(w, h, 0);
  89.     if (!image) {
  90.     return NULL;
  91.     }
  92.     ptr = image->data;
  93.     if (!raw) {
  94.     
  95.     } else {
  96.     if (max<256) {
  97.         i = 0;
  98.         while (i < w*h) {
  99.         if (fread(buf, 1, 3, file)!=3)
  100.             goto short_file;
  101.         *(ptr++) = buf[0];
  102.         *(ptr++) = buf[1];
  103.         *(ptr++) = buf[2];
  104.         i++;
  105.         }
  106.     } else {
  107.         
  108.     }
  109.     }
  110.     
  111.     return image;
  112.     
  113.   short_file:
  114.     RErrorCode = RERR_BADIMAGEFILE;
  115.     return NULL;
  116. }
  117.  
  118.  
  119. RImage*
  120. RLoadPPM(RContext *context, char *file_name, int index)
  121. {
  122.     FILE *file;
  123.     RImage *image = NULL;
  124.     char buffer[256];
  125.     int w, h, m;
  126.     int type;
  127.  
  128. #define GETL() if (!fgets(buffer, 255, file)) goto short_file
  129.     
  130.     file = fopen(file_name, "r");
  131.     if (!file) {
  132.     RErrorCode = RERR_OPEN;
  133.     return NULL;
  134.     }
  135.     
  136.     /* get signature */
  137.     GETL();
  138.  
  139.     /* only accept raw pixmaps or graymaps */
  140.     if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
  141.     RErrorCode = RERR_BADFORMAT;
  142.     fclose(file);
  143.     return NULL;
  144.     }
  145.     
  146.     type = buffer[1];
  147.     
  148.     /* skip comments */
  149.     while (1) {
  150.     GETL();
  151.     
  152.     if (buffer[0]!='#')
  153.         break;
  154.     }
  155.     
  156.     /* get size */
  157.     if (sscanf(buffer, "%i %i", &w, &h)!=2)
  158.     goto bad_file;
  159.     
  160.  
  161.     GETL();
  162.     if (sscanf(buffer, "%i", &m)!=1 || m < 1)
  163.     goto bad_file;
  164.  
  165.     if (type=='5')
  166.     image = load_graymap(file_name, file, w, h, m, type=='5');
  167.     else if (type=='6')
  168.     image = load_pixmap(file_name, file, w, h, m, type=='6');
  169.  
  170.     fclose(file);
  171.     return image;
  172.     
  173.   bad_file:
  174.   short_file:
  175.     RErrorCode = RERR_BADIMAGEFILE;
  176.     fclose(file);
  177.     return NULL;
  178. }
  179.  
  180.